/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package opensocial.apps.friendforsale.command;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import opensocial.apps.common.Message;
import opensocial.apps.friendforsale.model.Activity;
import opensocial.apps.friendforsale.model.ActivityType;
import opensocial.apps.friendforsale.model.Player;

import org.apache.log4j.Logger;

/**
 * viewer ransom himself/herself
 * 
 * @author Sirius
 * 
 */
public class RansomCommand extends FriendsaleBaseCommand {
  private static final Logger LOGGER = Logger.getLogger(RansomCommand.class);

  @Override
  public boolean execute() throws Exception {
    Player viewer = this.getViewer();
    if (viewer == null) {
      LOGGER.error("parameter error: not found viewer:" + ctn + " " + vid);
      output(new Message(MessageId.PARAM_ERROR, null));
      return END;
    }

    if (viewer.getMasterid() == null) {
      LOGGER.error("ransom error:your don`t have master");
      output(new Message(MessageId.DONT_HAVE_MASTER, null));
      return END;
    }

    Player master = this.getPlayer(viewer.getMasterid());
    if (master == null) {
      LOGGER.error("parameter error: not found master:" + ctn + " "
          + viewer.getMasterid());
      output(new Message(MessageId.PARAM_ERROR, null));
      return END;
    }

    if (viewer.getCash() < viewer.getPrice()) {
      LOGGER.info("not enough money.");
      output(new Message(MessageId.NOT_ENOUGH_MONEY, null));
      return END;
    }

    this.hasError(false);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("viewer", viewer);
    params.put("master", master);

    viewer.setCash(viewer.getCash() - viewer.getPrice());
    viewer.setCapital(viewer.getCapital() - viewer.getPrice());
    viewer.setMasterid(null);
    viewer.setMastername(null);
    viewer.setNickname(null);

    master.setCash(master.getCash() + viewer.getPrice());
    master.setCapital(master.getCapital() + viewer.getPrice());
    master.setSlavenum(master.getSlavenum() - 1);

    Activity activity = new Activity();
    activity.setActor(viewer.getUserId());
    activity.setActorName(viewer.getName());
    activity.setContainer(viewer.getContainer());
    activity.setCommand(MessageId.RANSOM);
    activity.setTime(new Date().getTime());
    activity.setTarget(master.getUserId());
    activity.setTargetName(master.getName());
    activity.setType(ActivityType.BUY);

    persist.save(viewer);
    persist.save(master);
    persist.save(activity);

    output(new Message(MessageId.RANSOM, params));
    LOGGER.info("viewer : " + vid + " ransom self from "
        + master.getUserId());
    return NEXT;

  }
}
